﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rail.Base
{
    public class RequestEx
    {
        private string UserName
        {
            get
            {
                return "admin";
            }
        }
        private string Password
        {
            get
            {
                return "admin123";
            }
        }
        private string BaseServer
        {
            get
            {
                return @"http://127.0.0.1/app";
            }
        }
        private RestClient m_RestClient;
        private RestClient RestClientInstance
        {
            get
            {
                if (m_RestClient == null) { 
                    m_RestClient = new RestClient();
                }
                return m_RestClient;
            }
        }

        /// <summary>
        /// 同步执行并返回字符串结果
        /// </summary>
        /// <param name="resource"></param>
        /// <returns></returns>
        public string Get(string resource, Dictionary<string ,string> theParams)
        {
            m_RestClient.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator(UserName, Password);
            var request = new RestRequest(resource,Method.GET);
            request.AddHeader("Content-Type", "application/json");
            request.Timeout = 10000;
            request.AddHeader("Cache-Control", "no-cache");
            foreach(var k in theParams.Keys)
            {
                request.AddParameter(k,theParams[k]);
            }

            var response = m_RestClient.Execute(request);
            var content = response.Content;

            return content;
        }
    }
}
